import co.flante.services.EntityManagerProvider;
import com.google.inject.AbstractModule;
import play.GlobalSettings;

import com.google.inject.Guice;
import com.google.inject.Injector;
import play.mvc.Action;
import play.mvc.Http;

import java.lang.reflect.Method;

/**
 * <p>Title: Play Framework global settings</p>
 * <p>Description: The Play Framework invokes this class on every request.</p>
 *
 * @author Mike Haas
 * @email michael.haas@my.uwrf.edu
 * @date November 17th 2013
 */
public class Global extends GlobalSettings {

    private static final Injector INJECTOR = createInjector();

    @Override
    public Action onRequest(Http.Request request, Method method) {
        return super.onRequest(request, method);
    }

    /**
     * Will set up Guice dependency injection no matter what controller is invoked from the route.
     * This boot straps the applications so that Guice can create an object graph to know where to inject
     * dependencies.
     * @param controllerClass
     * @param <A>
     * @return
     * @throws Exception
     */
    @Override
    public <A> A getControllerInstance(Class<A> controllerClass) throws Exception {
        return INJECTOR.getInstance(controllerClass);
    }

    /**
     * Where the magic happens for dependency injection binding.
     * @return
     */
    private static Injector createInjector() {

        return Guice.createInjector(new AbstractModule() {
            @Override
            protected void configure() {
                bind(EntityManagerProvider.class);
            }
        });
    }



}